//
//  BaseViewController.swift
//  swiftdemo
//
//  Created by liuqingyuan on 2018/11/12.
//  Copyright © 2018 liuqingyuan. All rights reserved.
//

import UIKit
import Toast_Swift
import ESPullToRefresh

class BaseViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        //        文件保存成功
        NotificationCenter.default.addObserver(self, selector: #selector(FileSaveSuccessNotification(notification:)), name: NSNotification.Name(rawValue: "FileSaveSuccessNotification"), object: nil)
        // 文件保存失败
        NotificationCenter.default.addObserver(self, selector: #selector(FileSaveFieldFileNotification(notification:)), name: NSNotification.Name(rawValue: "FileSaveFieldFileNotification"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(FileExistsNotification(notification:)), name: NSNotification.Name(rawValue: "FileExistsNotification"), object: nil)
        
        view.backgroundColor = UIColor.systemBackground
        ToastManager.shared.position = .center
    }
    @objc func FileSaveSuccessNotification(notification:Notification){
        let info = notification.userInfo
        let fileName:String = info!["fileName"] as! String
        Tool.showSystemAlert(viewController:self,title: "提示", message: "文件\(fileName)保存成功") {
        }
    }
    @objc func FileSaveFieldFileNotification(notification:Notification){
        let info = notification.userInfo
        let fileName:String = info!["fileName"] as! String
        Tool.showSystemAlert(viewController:self,title: "提示", message: "文件\(fileName)保存失败") {
        }
    }
    
    @objc func FileExistsNotification(notification:Notification){
        let info = notification.userInfo
        let fileName:String = info!["fileName"] as! String
        Tool.showSystemAlert(viewController:self,title: "提示", message: "文件\(fileName)已存在") {
        }
    }
        
    func setNavColor(navColor:UIColor,titleColor:UIColor,barStyle:UIBarStyle) {
        navigationController?.isNavigationBarHidden = false
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.barStyle = barStyle
        navigationController?.navigationBar.barTintColor = navColor
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:titleColor]
        navigationController?.navigationBar.tintColor = titleColor
        let statusBarView = UIView(frame: view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBarView.backgroundColor = navColor
        view.addSubview(statusBarView)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    var header: ESRefreshHeaderAnimator {
        get {
            let h = ESRefreshHeaderAnimator.init(frame: CGRect.zero)
            h.pullToRefreshDescription = "下拉刷新"
            h.releaseToRefreshDescription = "松开获取最新数据"
            h.loadingDescription = "下拉刷新..."
            return h
        }
    }
    
    var footer: ESRefreshFooterAnimator {
        get {
            let f = ESRefreshFooterAnimator.init(frame: CGRect.zero)
            f.loadingMoreDescription = "上拉加载更多"
            f.noMoreDataDescription = ""
            f.loadingDescription = "加载更多..."
            return f
        }
    }
}
